﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<table id="listCuNgu">
</table>
<div id="pagerCuNgu">
</div>
<script type="text/javascript">
    function LoadCuNgu() {
        var grid = $("#listCuNgu");
        grid.jqGrid({
            url: '<%= Url.Action("Get_List_GiaoDan_CuNgu", "GiaDinhCongGiao",new {id=ViewData["ID"]}) %>',
            datatype: 'json',
            mtype: 'POST',
            colNames: [
            '#',
            'Năm',
            'Địa Chỉ',
            'Giáo Phận',
            'Giáo Hạt',
            'Giáo Xứ'
                    ],
            colModel: [
            { name: 'MADIACHI', index: 'MADIACHI', width: 150, align: 'left', sortable: false, search: false, key: true, hidden: true },
            { name: 'NAM', index: 'NAM', width: 150, align: 'center', sortable: false, search: false },
            { name: 'DIACHI', index: 'DIACHI', width: 150, align: 'left', sortable: false, search: false },
            { name: 'TENGIAOPHAN', index: 'TENGIAOPHAN', width: 150, align: 'left', sortable: false, search: false },
            { name: 'TENGIAOHAT', index: 'TENGIAOHAT', width: 150, align: 'left', sortable: false, search: false },
            { name: 'TENGIAOXU', index: 'TENGIAOXU', width: 150, align: 'left', sortable: false, search: false }
            ],
            jsonReader: {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems: false,
                userdata: "userdata"
            },

            multiselect: true,
            multiboxonly: true,
            pager: $("#pagerCuNgu"),
            toppager: true,
            autowidth: false,
            width: '800',
            rowNum: 15,
            rowList: [10, 15, 20, 30, 60],
            sortname: 'NAM',
            sortorder: 'asc',
            viewrecords: true,
            height: 'auto',
            caption: ''
        })
        .navGrid('#pagerCuNgu', { view: false, del: false, add: false, edit: false, searchtext: 'Tìm kiếm', cloneToTop: true },
                {}, // default settings for edit
                {}, // default settings for add
                {}, // delete instead that del:false we need this
                {closeOnEscape: true, multipleSearch: true, closeAfterSearch: true }, // search options
                {} /* view parameters*/
        );


        grid.jqGrid('navButtonAdd', '#' + grid[0].id + '_toppager_left', { // "#listCuNgu_toppager_left"
            caption: "",
            title: "Tạo mới",
            buttonicon: 'ui-icon-plus',
            onClickButton: function () {
                window.location = '<%=Url.Action("AddGiaoDan_CuNgu", "GiaDinhCongGiao",new {id=ViewData["ID"]})%>';
            }
        });

        grid.jqGrid('navButtonAdd', '#' + grid[0].id + '_toppager_left', { // "#listCuNgu_toppager_left"
            caption: "",
            buttonicon: 'ui-icon-pencil',
            onClickButton: function () {
                var selrows = grid.getGridParam("selarrrow");
                if (selrows != null) {
                    if (selrows.length > 1) {
                    }
                    else if (selrows.length == 0) {
                    }
                    else {
                        var url = '<%=Url.Action("EditGiaoDan_CuNgu", "GiaDinhCongGiao", new { id="ID" })%>';
                        window.location = url.replace("ID", selrows);
                    }
                }

            }
        });

        grid.jqGrid('navButtonAdd', '#' + grid[0].id + '_toppager_left', { // "#listCuNgu_toppager_left"
            caption: "",
            buttonicon: 'ui-icon-trash',
            onClickButton: function () {
                var selrows = grid.getGridParam("selarrrow");
                var rows = "";

                for (var i = 0; i < selrows.length; i++) {
                    if (selrows[i] != null) {
                        if (rows.length <= 0)
                            rows += selrows[i];
                        else
                            rows += "," + selrows[i];
                    }
                }

                if (rows.length > 0) {
                    var comfirm = confirm('Do you want to delete?');
                    if (comfirm) {
                        $.post('<%=Url.Action("DeleteGiaoDan_CuNgu", "GiaDinhCongGiao") %>', { id: rows },
                        function (data) {
                            $('#listCuNgu').trigger("reloadGrid", function () {
                                data;
                            });
                        });
                    }
                }
                else {
                }
            }
        });

        // remove some double elements from one place which we not need double
        var topPagerDiv = $('#' + grid[0].id + '_toppager')[0];         // "#listCuNgu_toppager"
        $("#edit_" + grid[0].id + "_top", topPagerDiv).remove();        // "#edit_listCuNgu_top"
        $("#del_" + grid[0].id + "_top", topPagerDiv).remove();         // "#del_listCuNgu_top"
        $("#search_" + grid[0].id + "_top", topPagerDiv).remove();      // "#search_listCuNgu_top"
        $("#refresh_" + grid[0].id + "_top", topPagerDiv).remove();     // "#refresh_listCuNgu_top"
        $("#" + grid[0].id + "_toppager_center", topPagerDiv).remove(); // "#listCuNgu_toppager_center"
        $(".ui-paging-info", topPagerDiv).remove();

        var bottomPagerDiv = $("div#pagerCuNgu")[0];
        $("#add_" + grid[0].id, bottomPagerDiv).remove();               // "#add_listCuNgu"

    }
     
</script>
